﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Classified.Data.Infrastructure;
using Classified.Domain.Entities;


namespace Classified.Data.Repositories
{
    public class ClassifiedAdCategoryRepository : RepositoryBase<ClassifiedCategory>, IClassifiedAdCategoryRepository
    {
        public ClassifiedAdCategoryRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }


    }
    public class ClassifiedAdRepository : RepositoryBase<ClassifiedAd>, IClassifiedRepository
    {
        private readonly ILocationRepository _locationRepository;
        private readonly IAttributeValueRepository _attributeValueRepository;
        public ClassifiedAdRepository(IDatabaseFactory databaseFactory, ILocationRepository locationRepository, IAttributeValueRepository attributeValueRepository)
            : base(databaseFactory)
        {
            _locationRepository = locationRepository;
            _attributeValueRepository = attributeValueRepository;

        }
        
        public IEnumerable<Location> Locations()
        {
            var data = _locationRepository.GetAll();
            return data;
        }
        public IEnumerable<AttributeValue> AttributeValues()
        {
            var data = _attributeValueRepository.GetAll();
            return data;
        }
        public void Assignlocations(int id, int locations)
        {
            var classified = this.GetById(id);
            if (classified.Locations != null)
            {
                classified.Locations.Clear();
            }
            else
            {
                classified.Locations = new List<Location>();
            }

            var location = this.DataContext.Location.Find(locations);
                classified.Locations.Add(location);
           

            this.DataContext.ClassifiedAds.Attach(classified);
            this.DataContext.Entry(classified).State = EntityState.Modified;
            this.DataContext.SaveChanges();
        }
        public void AssignUser(int id, int userId)
        {
            var currentUser = this.GetById(id);
            if (currentUser.Users != null)
            {
                currentUser.Users.Clear();
            }
            else
            {
                currentUser.Users = new List<User>();
            }

            var user = this.DataContext.Users.Find(userId);
            currentUser.Users.Add(user);


            this.DataContext.ClassifiedAds.Attach(currentUser);
            this.DataContext.Entry(currentUser).State = EntityState.Modified;
            this.DataContext.SaveChanges();
        }

        public void AssignAttributeVaues(int id, List<int> attributes)
        {
            var classified = this.GetById(id);
            if (classified.AttributeValues != null)
            {
                classified.AttributeValues.Clear();
            }
            else
            {
                classified.AttributeValues = new List<AttributeValue>();
            }
            foreach (var item in attributes)
            {
                var data = this.DataContext.AttributeValues.Find(item);
                classified.AttributeValues.Add(data);
            }

            this.DataContext.ClassifiedAds.Attach(classified);
            this.DataContext.Entry(classified).State = EntityState.Modified;
            this.DataContext.SaveChanges();
        }

        public List<ClassifiedAd> GetClassifiedAdsByLocationCategoryAttributes(string categoryId, string andParam,string locationId)
        {
           // var sql = "SearchByAttributes @attrubuteID='" + andParam + "', @categoryId='" + categoryId + "'";
            var data = DataContext.ClassifiedAds.SqlQuery("GetClassifiedAdsByLocationCategoryAttributes @attrubuteID='" + andParam + "', @categoryId='" + categoryId + "',@locationId='" + locationId + "'").ToList();
            
            return data;
        }
        public List<ClassifiedAd> GetClassifiedByUserId(int userId)
        {
            var data = DataContext.ClassifiedAds.SqlQuery("Sp_ClassifiedByUserId @userId='" + userId + "'").ToList();
            return data;
        }
        public List<ClassifiedAd> ClassifiedInActiveByUserId(int userId)
        {
            var data = DataContext.ClassifiedAds.SqlQuery("Sp_ClassifiedInActiveByUserId @userId='" + userId + "'").ToList();
            return data;
        }
        


        public List<ClassifiedAd> SearchByTextValue(string textvalue,string categoryid,string locationId)
        {
            textvalue = textvalue.Replace("'", "''");
            var sql = " exec SearchByText @TextValue='" + textvalue + "',@categoryId='" + categoryid + "',@locationId='" + locationId + "'";
            var parameter = new SqlParameter("@TextValue", textvalue);
            var parameter2 = new SqlParameter { ParameterName = "@categoryId", Value = categoryid };
            var parameter3 = new SqlParameter { ParameterName = "@locationId", Value = locationId };
            var data = DataContext.ClassifiedAds.SqlQuery("SearchByText @TextValue,@categoryId,@locationId",parameter, parameter2, parameter3).ToList();
            return data;
        }
    
       
    }

    public class ClassifiedPictureRepositories : RepositoryBase<ClassifiedPicture>, IClassifiedPictureRepositories
    {
        public ClassifiedPictureRepositories(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }

    }

    public class ClassifiedCommentRepository : RepositoryBase<ClassifiedComment>, IClassifiedCommentRepository
    {
        public ClassifiedCommentRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }


    }

    public interface IClassifiedCommentRepository : IRepository<ClassifiedComment>
    {


    }

    public interface IClassifiedAdCategoryRepository : IRepository<ClassifiedCategory>
    {


    }
    public interface IClassifiedRepository : IRepository<ClassifiedAd>
    {
        void Assignlocations(int id, int locations);
        IEnumerable<Location> Locations();
        void AssignUser(int id, int userId);
        void AssignAttributeVaues(int id, List<int> attributes);
        IEnumerable<AttributeValue> AttributeValues();

        List<ClassifiedAd> GetClassifiedAdsByLocationCategoryAttributes(string categoryId, string andParam,
                                                                        string locationId);
        List<ClassifiedAd> SearchByTextValue(string textvalue, string categoryid, string locationId);
        List<ClassifiedAd> GetClassifiedByUserId(int userId);
        List<ClassifiedAd> ClassifiedInActiveByUserId(int userId);

    }

    public interface IClassifiedPictureRepositories : IRepository<ClassifiedPicture>
    {


    }


}